package org.jfinanceobserver.persistence.jpa.controller;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.jfinanceobserver.persistence.jpa.controller.exceptions.NonexistentEntityException;
import org.jfinanceobserver.persistence.jpa.entities.QuotesActive;
import org.jfinanceobserver.persistence.jpa.entities.QuotesData;

/**
 * This class is the QuotesData Jpa Controller
 *
 * $Author: r78v10a07@gmail.com $ 
 * $LastChangedDate: 2013-02-12 09:59:23 +0000 (Tue, 12 Feb 2013) $ 
 * $LastChangedRevision: 19 $
 * @since Feb 11, 2013
 */
public class QuotesDataJpaController extends AbstractJpaController<QuotesData> implements Serializable {

    public QuotesDataJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    @Override
    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(QuotesData quotesData) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            QuotesActive activeid = quotesData.getActiveid();
            if (activeid != null) {
                activeid = em.getReference(activeid.getClass(), activeid.getId());
                quotesData.setActiveid(activeid);
            }
            em.persist(quotesData);
            if (activeid != null) {
                activeid.getQuotesDataCollection().add(quotesData);
                activeid = em.merge(activeid);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(QuotesData quotesData) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            QuotesData persistentQuotesData = em.find(QuotesData.class, quotesData.getId());
            QuotesActive activeidOld = persistentQuotesData.getActiveid();
            QuotesActive activeidNew = quotesData.getActiveid();
            if (activeidNew != null) {
                activeidNew = em.getReference(activeidNew.getClass(), activeidNew.getId());
                quotesData.setActiveid(activeidNew);
            }
            quotesData = em.merge(quotesData);
            if (activeidOld != null && !activeidOld.equals(activeidNew)) {
                activeidOld.getQuotesDataCollection().remove(quotesData);
                activeidOld = em.merge(activeidOld);
            }
            if (activeidNew != null && !activeidNew.equals(activeidOld)) {
                activeidNew.getQuotesDataCollection().add(quotesData);
                activeidNew = em.merge(activeidNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = quotesData.getId();
                if (findQuotesData(id) == null) {
                    throw new NonexistentEntityException("The quotesData with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            QuotesData quotesData;
            try {
                quotesData = em.getReference(QuotesData.class, id);
                quotesData.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The quotesData with id " + id + " no longer exists.", enfe);
            }
            QuotesActive activeid = quotesData.getActiveid();
            if (activeid != null) {
                activeid.getQuotesDataCollection().remove(quotesData);
                activeid = em.merge(activeid);
            }
            em.remove(quotesData);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<QuotesData> findQuotesDataEntities() {
        return findQuotesDataEntities(true, -1, -1);
    }

    public List<QuotesData> findQuotesDataEntities(int maxResults, int firstResult) {
        return findQuotesDataEntities(false, maxResults, firstResult);
    }

    private List<QuotesData> findQuotesDataEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(QuotesData.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public QuotesData findQuotesData(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(QuotesData.class, id);
        } finally {
            em.close();
        }
    }

    public int getQuotesDataCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<QuotesData> rt = cq.from(QuotesData.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
